﻿#include "filetrans_server.h"
#include "filetrans_socket.h"

FileTrans::FileTrans()
{

}

FileTrans::~FileTrans()
{
    for(auto x:m_FileTrans_list)
    {
        if(x)
        {
            x->disconnect();
            x->close();
        }
    }
}

FileTrans& FileTrans::getInstance()
{
    static FileTrans filetrans;
    return filetrans;
}

void FileTrans::incomingConnection(qintptr handle)
{
    FileTrans_Socket* socket = new FileTrans_Socket;
    socket->setSocketDescriptor(handle);
    m_FileTrans_list.append(socket);
    qDebug()<<"have file trans client connected"<<endl;
}

void FileTrans::TransFileData(char* recvname, QString filepath,QString filename, uint filesize)
{
    for(auto& x:m_FileTrans_list)
    {
        if(strcmp(x->m_u.pwd,recvname)==0)//找到接收人
        {

            FileThread* ft = new FileThread(filepath,filename,x,filesize);
            m_filethreadlist.append(ft);
            ft->run();
            return;
        }
    }

}

